﻿using System.Collections.Generic;
using System.Data.SqlClient;
using KMF.Common;

namespace KMF.DAL
{
    public partial class PageDb : DbBase
    {
        /// <summary>
        /// Lấy về danh sách bản ghi theo trường PageKey
        /// </summary>
        /// <param name="pageKey"></param>
        /// <returns></returns>
        public static List<PageDto> GetByKey(string pageKey)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_Page_GetByKey");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@PageKey", pageKey));
                return dao.GetDTOList<PageDto>(ref command);
            }
            finally
            {
                command.Connection.Close();
            }
        }

        /// <summary>
        /// Thêm bản ghi và lấy về PageId vừa được Insert vào
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static int AddPage(PageDto model)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_Page_Add");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@Name", model.Name));
                command.Parameters.Add(dao.CreateParameter("@PageKey", model.PageKey));
                command.Parameters.Add(dao.CreateParameter("@CreateDate", model.CreateDate));
                command.Parameters.Add(dao.CreateParameter("@Order", model.Order));
                var result = (int)command.ExecuteScalar();
                return result;
            }
            finally
            {
                command.Connection.Close();
            }
        }//End method

        /// <summary>
        /// Xóa page
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static int DeletePage(int id)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_Page_DeleteByPageId");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@PageId", id));
                var result = command.ExecuteNonQuery();
                return result;
            }
            finally
            {
                command.Connection.Close();
            }
        }
    }
}
